package com.example.demo.config;

import com.example.demo.context.TxTransactionConstants;
import com.example.demo.listen.TxTransactionListen;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
import org.springframework.scheduling.annotation.EnableScheduling;

/**
 * @author yuaneg 2018/12/13
 */
@Configuration
@EnableScheduling
public class TxTransactionConfiguration {

    @Bean
    public MessageListenerAdapter messageListenerAdapterSuccess(TxTransactionListen txTransactionListen) {
        return new MessageListenerAdapter(txTransactionListen, "transactionListenSuccess");
    }

    @Bean
    public MessageListenerAdapter messageListenerAdapterFail(TxTransactionListen txTransactionListen) {
        return new MessageListenerAdapter(txTransactionListen, "transactionListenFail");
    }

    @Bean
    public RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory,
                                                   MessageListenerAdapter messageListenerAdapterSuccess,
                                                   MessageListenerAdapter messageListenerAdapterFail) {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        container.addMessageListener(messageListenerAdapterSuccess, new PatternTopic(TxTransactionConstants.TX_NOTIFY_CHAT_SUCCESS));
        container.addMessageListener(messageListenerAdapterFail, new PatternTopic(TxTransactionConstants.TX_NOTIFY_CHAT_FAIL));
        return container;
    }

}
